﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

//此脚本挂载的游戏物体是用于复制的，其上不应该作画
public class CTileMap_Canvas : CTilemap
{
    [SerializeField]
    private TilemapData tilemapData;

    /// <summary>
    /// 画一片Tile
    /// </summary>
    /// <param name="tilePositions">Cell坐标</param>
    /// <param name="tiles">要画的Tile</param>
    /// <param name="t">留存时间，若为0，不主动擦除会永久留存</param>
    public void DrawTile(List<Vector3Int> tilePositions, List<TileBase> tiles, float t = 0)
    {
        for (int i = 0; i < tilePositions.Count; i++)
        {
            this[tilePositions[i]] = tiles[i];
        }
        if (t > 0)
        {
            StartCoroutine(EraseTile(tilePositions, t));
        }
    }
    private IEnumerator EraseTile(List<Vector3Int> tilePositions,float t)
    {
        yield return new WaitForSeconds(t);
        for (int i = 0; i < tilePositions.Count; i++)
        {
            this[tilePositions[i]] = null;
        }
    }
    /// <summary>
    /// 擦除一片Tile
    /// </summary>
    public void EraseAll()
    {
        List<Vector3Int> tilePositions = GetAllPosition();
        DrawTile(tilePositions, null);
    }

    /// <summary>
    /// 画一片光效
    /// </summary>
    /// <param name="tilePositions">Cell坐标</param>
    /// <param name="tiles_platform">平台的Tile，一一对应为光效的Tile</param>
    /// <param name="t">留存时间，若为0，不主动擦除会永久留存</param>
    public void DrawLight(List<Vector3Int> tilePositions,List<TileBase> tiles_platform,float t =0)
    {
        List<TileBase> tiles_light = new List<TileBase>();
        tilemapData.GetTiles(tiles_light, tiles_platform);
        DrawTile(tilePositions, tiles_light, t);
    }
}
